package com.lunduhotel.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.lunduhotel.factory.ConnectionFactory;
import com.lunduhotel.modelo.Reserva;

public class ReservaDao {
	
	private Connection con;
	
	public ReservaDao(){
		
		ConnectionFactory factory = new ConnectionFactory();
		con = factory.getConnection();
	}
	
	
	public void criarReservas(Reserva reserva){
		try {
			String sql = "insert into reservas(data_entrada,data_saida,id_quarto,id_cliente) values (?,?,?,?);";
			PreparedStatement stmt;
			stmt = con.prepareStatement(sql);
			
			stmt.setDate(1, new Date(reserva.getDataEntrada().getTimeInMillis()));
			stmt.setDate(2, new Date(reserva.getDataEntrada().getTimeInMillis()));
			stmt.setInt(3, reserva.getIdQuarto());
			stmt.setInt(4, reserva.getIdCliente());
			
			stmt.execute();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		
	}
	
	public void atualizarReservas(Reserva reserva){
		try {
			String sql = "update reservas set data_entrada=?,data_saida=?,id_quarto=?,id_cliente=?;";
			PreparedStatement stmt;
			stmt = con.prepareStatement(sql);
			
			stmt.setDate(1, new Date(reserva.getDataEntrada().getTimeInMillis()));
			stmt.setDate(2, new Date(reserva.getDataEntrada().getTimeInMillis()));
			stmt.setInt(3, reserva.getIdQuarto());
			stmt.setInt(4, reserva.getIdCliente());
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		
	}
	
	public void removerReservas(Reserva reserva){
		try {
			String sql = "delete from reservas where data_entrada=?,data_saida=?,id_quarto=?,id_cliente=?;";
			PreparedStatement stmt;
			stmt = con.prepareStatement(sql);
			
			stmt.setDate(1, new Date(reserva.getDataEntrada().getTimeInMillis()));
			stmt.setDate(2, new Date(reserva.getDataEntrada().getTimeInMillis()));
			stmt.setInt(3, reserva.getIdQuarto());
			stmt.setInt(4, reserva.getIdCliente());
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		
	}
	
}
